package com.weipai.demo.controller;

import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.PayOrderMapper;
import com.weipai.demo.model.PayOrder;
import com.weipai.demo.util.AliPayTXUtil;
import com.weipai.demo.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

@RestController
public class AlipayController {
//    private static final Logger logger = LoggerFactory.getLogger(AlipayController.class);
    @Autowired
    private PayOrderMapper payOrderMapper;
//    private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(AlipayController.class);

    @RequestMapping("/Alipay/query")
    public Response AliPayQuery(String phone){
        Response response1 = new Response();
        if (phone.isEmpty())
        {
            response1.setCode(RetCode.PAY_FAILD);
            return response1;
        }
        PayOrder pay = new PayOrder();
        pay.setMobile(phone);
        PayOrder payOrder = payOrderMapper.selectOne(pay);
        return AliPayTXUtil.AliPagePay(payOrder);
    }

    @RequestMapping("/AliPay/returnUrl")
    public void AliPayReturn(HttpServletRequest request) throws  UnsupportedEncodingException {

        AliPayTXUtil.validateAliPay(request);
    }

    @RequestMapping("/AliPay/notifyUrl")
    public void AliPayNotify(HttpServletRequest request) throws  UnsupportedEncodingException {

//        AliPayTXUtil.validateAliPay(request);
    }

}
